package com.ottoman.snackgames;

import java.util.ArrayList;
import java.util.Iterator;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

public class simpleMaze implements ApplicationListener {
	
	SpriteBatch spriteBatch;
	BitmapFont font;
	Sketch shape;
	World world ;
	Body theBall ;Sprite sBall;
	Vector2 scr;
    protected OrthographicCamera camera;
    protected Box2DDebugRenderer renderer;
    
    float ballRad = 10;
    MazeWorld mw = new MazeWorld();
    
	@Override
	public void create() {
		scr = new Vector2(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		camera = new OrthographicCamera(480, 320);    
	    camera.position.set(0.5f, 0.5f, 0);
		world = new World(new Vector2(0, 0), true);
		mw.setWorld(world);
		renderer = new Box2DDebugRenderer( true, true, true, true ); 
		font = new BitmapFont();
		font.setColor(Color.WHITE);
		spriteBatch = new SpriteBatch();
		sBall = new Sprite( new Texture(Gdx.files.internal("data/ball.png")));
		shape = new Sketch();
		shape.setWorld(world);
		
		shape.scale = (int)Math.round((scr.y - 50) / (shape.mazeHeight + 1));
		ballRad = Math.round((shape.scale - shape.spacing - 2) / 2);
		shape.setup();
		theBall = makeBall();
		mw.makeBoundry(scr);
		shape.builtMazeBody();

	}

	private Body makeBall(){
		BodyDef bodyDef0 = new BodyDef();
		bodyDef0.type = BodyType.DynamicBody;
		FixtureDef fixDef1 = new FixtureDef();
		fixDef1.density = 5;
		fixDef1.friction = 0.3f;
		fixDef1.restitution = 0f;
		fixDef1.shape = new CircleShape();
		fixDef1.shape.setRadius(ballRad * mw.PIX_TO_METER);
		bodyDef0.position.set((scr.x - (ballRad * 2)) * mw.PIX_TO_METER, (scr.y - (ballRad * 2)) * mw.PIX_TO_METER);
		
		Body myBall=world.createBody(bodyDef0);
		myBall.createFixture(fixDef1);
		return myBall;
	}



	

	@Override
	public void render() {
		world.step(0.005f, 10, 10);
		GL10 gl = Gdx.app.getGraphics().getGL10();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		//gl.glClearColor(0.7f, 1f , 0.9f, 1);
		camera.update();
		camera.apply(gl);
		// render the world using the debug renderer
		//renderer.render(world, camera.view);
		
		spriteBatch.begin();
        Vector2 ballPos = mw.toScreen(theBall.getPosition());
		Vector2 ballSize =new Vector2(ballRad * 2, ballRad * 2);
		/*
		sBall.setBounds(ballPos.x - (ballSize.x/2), ballPos.y - (ballSize.y/2), ballSize.y, ballSize.y);
		sBall.setOrigin(sBall.getWidth()/2, sBall.getHeight()/2);
		sBall.setRotation(MathUtils.radiansToDegrees * theBall.getAngle());
		sBall.draw(spriteBatch);
		*/
		shape.begin(ShapeType.FilledCircle);
		shape.setColor(1, 0, 0, 0);
		
		shape.filledCircle(ballPos.x, ballPos.y, ballRad);
		shape.end();
		
		shape.begin(ShapeType.Rectangle);
		shape.setColor(1, 1, 1, 0);
		//Iterator<Body> bodies = world.getBodies();
        //while(bodies.hasNext()){
		ArrayList<Body> bodies = shape.bodies;
		for(int i=0;i<bodies.size();i++){
			Body b = bodies.get(i);
			if(b.getUserData()!=null){
			Vector2 d = (Vector2)b.getUserData();
			//shape.line(b.getPosition().x * METER_TO_PIX - (d.x / 2), b.getPosition().y * METER_TO_PIX - (d.y / 2), (b.getPosition().x * METER_TO_PIX) - (d.x / 2) + d.x, (b.getPosition().y * METER_TO_PIX) - (d.y / 2) + d.y);
			shape.rect(b.getPosition().x * mw.METER_TO_PIX - (d.x / 2), b.getPosition().y * mw.METER_TO_PIX - (d.y / 2), d.x, d.y);
			}
		}
		
        shape.end();
        
		
        theBall.applyForceToCenter(Gdx.input.getAccelerometerY() * theBall.getMass(), -Gdx.input.getAccelerometerX() * theBall.getMass());
        	
        
        
		spriteBatch.end();
	}

	@Override
	public void resize(int width, int height) {
		spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, width, height);
		//drd.myPos.set(0, 0);
		scr.x = width;
		scr.y = height;
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}
	
	@Override
	public void dispose () {
		shape.dispose();
		shape = null;
		font.dispose();
		spriteBatch.dispose();
		mw = null;
	}
	
}

